package me.goldze.mvvmhabit.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by alankin on 2016/8/11.
 */
public class TimeUtil {
    public static final long DAY_LONG = 24 * 60 * 60 * 1000;

    /**
     * 从时间(毫秒)中提取出日期
     *
     * @param millisecond
     * @return
     */
    public static String getDateFromMillisecond(long millisecond) {

        Date date = null;
        try {
            date = new Date(millisecond);
        } catch (Exception e) {
            e.printStackTrace();
        }


        Calendar current = Calendar.getInstance();


        ////今天
        Calendar today = Calendar.getInstance();

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));

        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);

        //昨天
        Calendar yesterday = Calendar.getInstance();

        yesterday.set(Calendar.YEAR, current.get(Calendar.YEAR));
        yesterday.set(Calendar.MONTH, current.get(Calendar.MONTH));
        yesterday.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH) - 1);
        yesterday.set(Calendar.HOUR_OF_DAY, 0);
        yesterday.set(Calendar.MINUTE, 0);
        yesterday.set(Calendar.SECOND, 0);


        // 今年
        Calendar thisYear = Calendar.getInstance();

        thisYear.set(Calendar.YEAR, current.get(Calendar.YEAR));
        thisYear.set(Calendar.MONTH, 0);
        thisYear.set(Calendar.DAY_OF_MONTH, 0);
        thisYear.set(Calendar.HOUR_OF_DAY, 0);
        thisYear.set(Calendar.MINUTE, 0);
        thisYear.set(Calendar.SECOND, 0);


        current.setTime(date);

        //今年以前
        if (current.before(thisYear)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = format.format(date);
            return dateStr;
        } else if (current.after(today)) {
            return "今天";
        } else if (current.before(today) && current.after(yesterday)) {
            return "昨天";
        } else {
            SimpleDateFormat format = new SimpleDateFormat("MM-dd");
            String dateStr = format.format(date);
            return dateStr;
        }
    }

    public static long getEndTime() {

        Calendar todayEnd = Calendar.getInstance();

        todayEnd.set(Calendar.HOUR_OF_DAY, 23);

        todayEnd.set(Calendar.MINUTE, 59);

        todayEnd.set(Calendar.SECOND, 59);

        todayEnd.set(Calendar.MILLISECOND, 999);

        return todayEnd.getTime().getTime();

    }

    public static boolean isWeekend() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar today = Calendar.getInstance();
        System.out.println("现在时间是 " + sdf.format(today.getTime().getTime()));
        if (today.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || today.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            return true;
        } else {
            return false;
        }
    }
    public static int getDayOfWeek(){
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int mWay = c.get(Calendar.DAY_OF_WEEK);
        if(mWay==1){
            return  7;
        }else if(mWay==2){
            return  1;
        }else if(mWay==3){
            return  2;
        }else if(mWay==4){
            return  3;
        }else if(mWay==5){
            return  4;
        }else if(mWay==6){
            return  5;
        }else if(mWay==7){
            return  6;
        }
        return  0;
    }
    /**
     * 从时间(毫秒)中提取出时间(时:分)
     * 时间格式:  时:分
     *
     * @param millisecond
     * @return
     */
    public static String getTimeFromMillisecond(long millisecond) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(millisecond);
        String timeStr = simpleDateFormat.format(date);
        return timeStr;
    }

    /**
     * 将毫秒转化成固定格式的时间
     * 时间格式: yyyy-MM-dd HH:mm:ss
     * 时间格式: yyyy-MM-dd
     *
     * @param millisecond
     * @return
     */
    public static String getDateTimeFromMillisecond(Long millisecond) {
        String dateStr = getDateTimeFromMillisecond(millisecond, "yyyy-MM-dd HH:mm:ss");
        return dateStr;
    }

    public static String getTimeFromMillisecond(Long millisecond) {
        String dateStr = getDateTimeFromMillisecond(millisecond, "yyyy-MM-dd");
        return dateStr;
    }

    public static String getDateTimeFromMillisecond(Long millisecond, String format) {
        if(millisecond==null||millisecond==0){
            return "--";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(millisecond);
        String dateStr = simpleDateFormat.format(date);
        return dateStr;
    }

    /**
     * 将时间转化成毫秒
     * 时间格式: yyyy-MM-dd HH:mm:ss
     *
     * @param time
     * @return
     */
    public static Long timeStrToSecond(String time) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Long second = format.parse(time).getTime();
            return second;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1l;
    }
    public static String timeStrToString(String time,String formatStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            Long second = format.parse(time).getTime();
            return getDateTimeFromMillisecond(second);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    /**
     * 获取时间间隔
     *
     * @param millisecond
     * @return
     */
    public static String getSpaceTime(long millisecond) {
        Calendar calendar = Calendar.getInstance();
        long currentMillisecond = calendar.getTimeInMillis();

        //间隔秒
        long spaceSecond = (currentMillisecond - millisecond) / 1000;

        //一分钟之内
        if (spaceSecond >= 0 && spaceSecond < 60*5) {
            return "刚刚";
        }
        //一小时之内
        else if (spaceSecond / 60 > 0 && spaceSecond / 60 < 60) {
            return spaceSecond / 60 + "分钟前";
        }
        //一天之内
        else if (spaceSecond / (60 * 60) > 0 && spaceSecond / (60 * 60) < 24) {
            return spaceSecond / (60 * 60) + "小时前";
        }
        //3天之内
        else if (spaceSecond / (60 * 60 * 24) > 0 ) {
            return spaceSecond / (60 * 60 * 24) + "天前";
        } else {
            return getDateTimeFromMillisecond(millisecond);
        }
    }

    /**
     * 获取时间间隔
     *
     * @param millisecond
     * @return
     */
    public static String getDur(long millisecond) {
//        Calendar calendar = Calendar.getInstance();
//        long currentMillisecond = calendar.getTimeInMillis();
        //间隔秒
//        long spaceSecond = (currentMillisecond - millisecond) / 1000;
        long spaceSecond = (millisecond) / 1000;

        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数

        long day = millisecond / nd;//计算差多少天
        long hour = millisecond % nd / nh;//计算差多少小时
        long min = millisecond % nd % nh / nm;//计算差多少分钟
        long sec = millisecond % nd % nh % nm / ns;//计算差多少秒
        String content = "<font color='red'>" + day + "</font>天<font color='red'>" + hour + "</font>时<font color='red'>" + min + "</font>分<font color='red'>" + sec + "</font>秒";

        //一小时之内
        if (spaceSecond / 60 > 0 && spaceSecond / 60 < 60) {
            return "<font color='red'>" + min + "</font>分<font color='red'>" + sec + "</font>秒";
        }
        //一天之内
        else if (spaceSecond / (60 * 60) > 0 && spaceSecond / (60 * 60) < 24) {
            return "<font color='red'>" + hour + "</font>时<font color='red'>" + min + "</font>分<font color='red'>" + sec + "</font>秒";
        }
        //3天之内
        else if (spaceSecond / (60 * 60 * 24) > 0) {
            return "<font color='red'>" + day + "</font>天<font color='red'>" + hour + "</font>时<font color='red'>" + min + "</font>分<font color='red'>" + sec + "</font>秒";

        } else if (spaceSecond > 0 && spaceSecond < 60) {
            return "<font color='red'>" + sec + "</font>秒";
        }
        return "<font color='red'>" + day + "</font>天<font color='red'>" + hour + "</font>时<font color='red'>" + min + "</font>分<font color='red'>" + sec + "</font>秒";
    }

    /**
     * 获取时间间隔
     *
     * @param minute
     * @return
     */
    public static String getDur(int minute) {
        //间隔秒
        Long spaceSecond = (long) minute * 60 * 1000;

        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long ns = 1000;//一秒钟的毫秒数

        long day = spaceSecond / nd;//计算差多少天
        long hour = spaceSecond % nd / nh;//计算差多少小时
        long min = spaceSecond % nd % nh / nm;//计算差多少分钟
        long sec = spaceSecond % nd % nh % nm / ns;//计算差多少秒
        //一小时之内
        if (spaceSecond / 60 > 0 && spaceSecond / 60 < 60) {
            return min + "分钟";
        }
        //一天之内
        else if (spaceSecond / (60 * 60) > 0 && spaceSecond / (60 * 60) < 24) {
            return hour + "小时" + min + "分钟";
        }
        //3天之内
        else if (spaceSecond / (60 * 60 * 24) > 0) {
            return day + "天" + hour + "小时" + min + "分钟";
        }
        return "0";
    }
    /************************日期工具类***********************/
    /***
     * 日期月份加减add个月
     *
     * @param datetime 日期(2014-11)
     * @return 2014-10
     */
    public static String dateFormat(String datetime, int add) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.MONTH, add);
        date = cl.getTime();
        return sdf.format(date);
    }
    public static String dateFormat(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(date);
    }
    public static String dateFormat1(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(date);
    }

    public static String dateFormat(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(date);
    }
    public static String dateFormat1(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    /****
     * 传入具体日期 ，返回具体日期减一个月。
     *
     * @param date 日期(2014-04-20)
     * @return 2014-03-20
     * @throws ParseException
     */
    public static String subMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);

        rightNow.add(Calendar.MONTH, -1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);

        return reStr;
    }

    public static int getYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    public static int getYear(long l) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(l);
        return calendar.get(Calendar.YEAR);
    }

    public static int getDay(long l) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(l);
        return calendar.get(Calendar.DATE);
    }

    public static int getDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }

    public static int getMonth() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getMonth(long l) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(l);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /****
     * 获取月末最后一天
     *
     * @param sDate 2014-11-24
     * @return 30
     */
    private static String getMonthMaxDay(String sDate) {
        SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        Date date = null;
        try {
            date = sdf_full.parse(sDate + "-01");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.setTime(date);
        int last = cal.getActualMaximum(Calendar.DATE);
        return String.valueOf(last);
    }

//    // 判断是否是月末
//    public static boolean isMonthEnd(Date date) {
//        Calendar cal = Calendar.getInstance();
//        cal.setTime(date);
//        if (cal.get(Calendar.DATE) == cal
//                .getActualMaximum(Calendar.DAY_OF_MONTH))
//            return true;
//        else
//            return false;
//    }

    /***
     * 日期减一天、加一天
     *
     * @param option 传入类型 pro：日期减一天，next：日期加一天
     * @param _date  2014-11-24
     * @return 减一天：2014-11-23或(加一天：2014-11-25)
     */
    public static String checkOption(String option, String _date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cl = Calendar.getInstance();
        Date date = null;

        try {
            date = (Date) sdf.parse(_date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cl.setTime(date);
        if ("pre".equals(option)) {
            // 时间减一天
            cl.add(Calendar.DAY_OF_MONTH, -1);

        } else if ("next".equals(option)) {
            // 时间加一天
            cl.add(Calendar.DAY_OF_YEAR, 1);
        } else {
            // do nothing
        }
        date = cl.getTime();
        return sdf.format(date);
    }

    /***
     * 判断日期是否为当前月， 是当前月返回当月最小日期和当月目前最大日期以及传入日期上月的最大日和最小日
     * 不是当前月返回传入月份的最大日和最小日以及传入日期上月的最大日和最小日
     *
     * @param date 日期 例如：2014-11
     * @return String[] 开始日期，结束日期，上月开始日期，上月结束日期
     * @throws ParseException
     */
    public static String[] getNow_Pre_Date(String date) throws ParseException {

        String[] str_date = new String[4];
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sdf_full = new SimpleDateFormat("yyyy-MM-dd");
        String stMonth = sdf.format(now);
        String stdate = "";// 开始日期
        String endate = "";// 结束日期
        String preDate_start = "";// 上月开始日期
        String preDate_end = "";// 上月结束日期

        // 当前月
        if (date.equals(stMonth)) {
            stdate = stMonth + "-01"; // 2014-11-01
            endate = sdf_full.format(now);// 2014-11-24
            preDate_start = subMonth(stdate);// 2014-10-01
            preDate_end = subMonth(endate);// 2014-10-24
        } else {
            // 非当前月
            String monthMaxDay = getMonthMaxDay(date);
            stdate = date + "-01";// 2014-10-01
            endate = date + "-" + monthMaxDay;// 2014-10-31
            preDate_start = subMonth(stdate);// 2014-09-01
            preDate_end = subMonth(endate);// 2014-09-30
        }
        str_date[0] = stdate;
        str_date[1] = endate;
        str_date[2] = preDate_start;
        str_date[3] = preDate_end;

        return str_date;
    }

    public static long getTodayPoint(int time) {
        Calendar current = Calendar.getInstance();

        ////今天
        Calendar today = Calendar.getInstance();
        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));

        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, time);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 1);
        return today.getTimeInMillis();
    }

    public static long getTodayPoint(int hour, int minute) {
        Calendar current = Calendar.getInstance();

        ////今天
        Calendar today = Calendar.getInstance();

        today.set(Calendar.YEAR, current.get(Calendar.YEAR));
        today.set(Calendar.MONTH, current.get(Calendar.MONTH));
        today.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));

        //  Calendar.HOUR——12小时制的小时数 Calendar.HOUR_OF_DAY——24小时制的小时数
        today.set(Calendar.HOUR_OF_DAY, hour);
        today.set(Calendar.MINUTE, minute);
        today.set(Calendar.SECOND, 1);
        return today.getTimeInMillis();
    }

    /**
     * 获取昨天的的某个时间点
     *
     * @param time
     * @return
     */
    public static long getTomorrowPoint(int time) {
        return getTodayPoint(time) - 24 * 60 * 60 * 1000;
    }

    /**
     * 获取当前时间的小时
     *
     * @return
     */
    public static int getCurHour() {
        Date date = new Date();
        return date.getHours();
    }


    public static long getStartMonth(int year, int month) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);

        calendar.set(Calendar.MINUTE, 0);

        calendar.set(Calendar.SECOND, 0);

        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime().getTime();
    }

    public static long getEndMonth(int year, int month) {

        Calendar calendar = Calendar.getInstance();
//        calendar.set(Calendar.YEAR, year);
//        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(year,month-1,15);
        int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        Log.e("bruce", "actualMaximum:>>>>>>>>> "+actualMaximum);
        calendar.set(Calendar.DAY_OF_MONTH, actualMaximum);

        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);

        calendar.set(Calendar.SECOND, 59);

        calendar.set(Calendar.MILLISECOND, 999);

        return calendar.getTime().getTime();
    }




}
